import Vue from 'vue';
import VueRouter from 'vue-router';
import {routers} from './router';
import iView from 'iview';
import App from '../lib/app';

Vue.use(VueRouter)

// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: routers,
  /* scrollBehavior (to, from, savedPosition) {
    return savedPosition ? savedPosition : {x: 0, y: 0}
  } */
};

export const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
  pageLoading();
  next();
});

router.afterEach((to) => {
  let title = (to.meta.title ? to.meta.title + ' - ' : '') + App.appName;
  document.title = title;
  // iView.LoadingBar.finish();
  iView.Spin.hide();
  window.scrollTo(0, 0);
});

const pageLoading = () => {
  iView.Spin.show({
    render: (h) => {
      return h('div', [
        h('Icon', {
          'class': 'spin-icon-load',
          style: {
            fontSize: '1rem'
          },
          props: {
            type: 'ios-loading'
          }
        }),
        h('div', {
          style: {
            fontSize: '.75rem'
          }
        }, '页面加载中……')
      ])
    }
  });
};
